import { defineStore } from 'pinia'
import request from '@/utils/request'

// 定义合同接口
interface Contract {
  id: number
  name: string
  code: string
  customer: string
  types: number
  cate_id: number
  did: number
  sign_uid: number
  sign_time: string
  start_time: string
  end_time: string
  keeper_uid: number
  prepared_uid: number
  share_ids: string
  file_ids: string
  amount: number
  content: string
  check_status: number
  archive_status: number
  stop_status: number
  void_status: number
  admin_id: number
  create_time: string
  update_time: string
  delete_time: number
  // 添加额外的显示字段
  types_name?: string
  status_name?: string
  cate_title?: string
  sign_name?: string
  interval_time?: string
  delay?: number
  keeper_name?: string
  archive_name?: string
  archive_time?: string
  stop_name?: string
  stop_time?: string
  void_name?: string
  void_time?: string
}

// 定义合同分类接口
interface ContractCategory {
  id: number
  title: string
  pid: number
  level: number
  sort: number
  admin_id: number
  create_time: string
}

// 定义API响应类型
interface ApiResponse<T> {
  code: number
  msg: string
  data: T
  count: number
}

interface ContractState {
  contracts: Contract[]
  contractTotal: number
}

// 合同相关API
const contractApi = {
  // 获取销售合同列表
  getContractList: (params: any): Promise<ApiResponse<Contract[]>> => {
    return request({
      url: '/api/contract/get_contract',
      method: 'get',
      params,
    })
  },

  // 销售合同归档操作
  contractArchive: (data: any) => {
    return request({
      url: '/api/contract/contract_archive',
      method: 'post',
      data,
    })
  },

  // 销售合同中止操作
  contractStop: (data: any) => {
    return request({
      url: '/api/contract/contract_stop',
      method: 'post',
      data,
    })
  },

  // 销售合同作废操作
  contractVoid: (data: any) => {
    return request({
      url: '/api/contract/contract_tovoid',
      method: 'post',
      data,
    })
  },

  // 获取合同分类列表
  getContractCategories: (params: any): Promise<ApiResponse<ContractCategory[]>> => {
    return request({
      url: '/api/contract/get_contract_categories',
      method: 'get',
      params,
    })
  },
}

export const useContractStore = defineStore('contract', {
  state: (): ContractState => ({
    contracts: [],
    contractTotal: 0,
  }),

  actions: {
    // 获取销售合同列表
    async getContractListAction(params: any) {
      try {
        const res = await contractApi.getContractList(params)
        if (res.code === 0) {
          this.contracts = res.data
          this.contractTotal = res.count
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 销售合同归档操作
    async contractArchiveAction(data: any) {
      try {
        const res = await contractApi.contractArchive(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 销售合同中止操作
    async contractStopAction(data: any) {
      try {
        const res = await contractApi.contractStop(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 销售合同作废操作
    async contractVoidAction(data: any) {
      try {
        const res = await contractApi.contractVoid(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取合同分类列表
    async getContractCategoriesAction(params: any) {
      try {
        const res = await contractApi.getContractCategories(params)
        return res
      } catch (error) {
        throw error
      }
    },
  },
})
